﻿/*Author：Lary Pan(itolmqj123@gmail.com)
 *Date  ：2016/7/15
 *Tips  ：Loading Manager
 * */
using System;
using DG.Tweening;

public class LoadingMgr : ISingleton<LoadingMgr>, IManager
{
    private UILoading uiLoading = null;

    public void Init() { }

    public void DoDestroy() { }

    public void ShowLoading()
    {
        if (uiLoading == null)
        {
            UIManager.Instance.OpenUI(GlobleConfig.ResPath.UILoading, uiBase => uiLoading = uiBase as UILoading);
        }

        if (!uiLoading.gameObject.activeSelf)
        {
            uiLoading.gameObject.SetActive(true);
        }
    }

    public void HideLoading()
    {
        if (uiLoading)
        {
            uiLoading.gameObject.SetActive(false);
            this.SetPercent(0);
        }
    }

    public void SetPercent(float p)
    {
        uiLoading.SetPercent(p);
    }

    /// <summary>
    /// 设置自动的进度条
    /// </summary>
    /// <param name="time">时间</param>
    public void ShowAotuLoading(float time, Action action = null)
    {
        this.ShowLoading();

        DOTween.To(p => SetPercent(p), 0, 1, time)
            .SetEase(Ease.Linear)
            .OnComplete(() => {
                HideLoading();
                if (action != null) action();
            });
                
               
    }
}
